#include <iostream>
#include "socket.hpp"
#include "netcal.hpp"
int main(int argc, char *argv[])
{
    if (argc != 3)
    {
        std::cerr << "usage : " << argv[0] << "server_ip  server_port" << std::endl;
        return -1;
    }
    std::shared_ptr<TcpSocket> tsvr = std::make_shared<TcpSocket>();
    tsvr->CreateTcpClientSocket();
    std::string server_ip = argv[1];
    uint16_t server_port = std::stoi(argv[2]);
    InetAddr server(server_ip, server_port);

    // 协议
    std::unique_ptr<Protocol> protocol = std::make_unique<Protocol>();

    // 发送连接请求
    tsvr->Connect(server);
    //
    std::string buffer;
    while (true)
    {
        // 1. 获取数据
        int x, y;
        char oper;
        std::cout << "Please enter x :";
        std::cin >> x;
        std::cout << "Please enter y :";
        std::cin >> y;
        std::cout << "Please enter oper :";
        std::cin >> oper;
        // 2. 构建请求 -> 结构化数据 -> 序列化数据 -> 添加协议报头的数据
        std::string send_massage = protocol->BuildComData(x, y, oper);
        // 3. 发送请求
        tsvr->Send(send_massage);
        std::cout << "**********send_massage************" << std::endl
                  << send_massage<< "**********************************" << std::endl;
        // 4. 获取应答
        Responce res;
        if (protocol->GetResponce(tsvr, buffer, &res))
            res.Show();
    }
    return 0;
}